package com.epalmpay.controller.channel.mall;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import com.epalmpay.util.Base64Utils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.epalmpay.commom.BaseController;
import com.epalmpay.exception.ApplicationException;
import com.epalmpay.status.Rescode;
import com.epalmpay.dto.system.PageDTO;
import com.epalmpay.dto.mall.MallGoodCommentInDTO;
import com.epalmpay.dto.mall.MallGoodCommentListOutDTO;
import com.epalmpay.dto.mall.MallGoodCommentOutDTO;
import com.epalmpay.entity.GroupConfig;
import com.epalmpay.entity.MallGoodComment;
import com.epalmpay.entity.Page;
import com.epalmpay.service.mall.IGroupConfigService;
import com.epalmpay.service.mall.IMallGoodCommentService;
import com.epalmpay.service.operator.ISysLogService;
import com.epalmpay.util.PropertyCopyUtil;
import com.epalmpay.util.Tools;

/** 
 * 说明：会员等级
 * 创建人：gwn
 * 创建时间：2017-05-24
 */
@Controller
@RequestMapping(value="/merchant/mall")
public class MallGoodCommentController  extends BaseController{

	@Resource
	private IMallGoodCommentService mallGoodCommentService;
	
	
	@Resource
	private IGroupConfigService groupConfigService;
	
	@Resource
	private ISysLogService sysLogService; 
 	/**
	 * 评级管理列表
	 * @param mallGoodCommentInDTO
	 * @return
	 */
	
	@RequestMapping(value="/mallGoodCommentList")
	@ResponseBody
	public MallGoodCommentListOutDTO  MallGoodCommentList(MallGoodCommentInDTO mallGoodCommentInDTO,PageDTO pageDTO){
		
		MallGoodCommentListOutDTO mallGoodCommentListOutDTO=new MallGoodCommentListOutDTO();
		try {
			Page page=Tools.pageEmtity(pageDTO);
			//获取所有评价列表信息
			List<MallGoodCommentOutDTO> mallGoodCommentOutList=mallGoodCommentService.MallGoodCommentList(mallGoodCommentInDTO,page);
			mallGoodCommentListOutDTO.setMallGoodCommentOutDTOList(mallGoodCommentOutList);
			mallGoodCommentListOutDTO.setPage(page);
			mallGoodCommentListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return mallGoodCommentListOutDTO;
			
		} 
		catch (ApplicationException ae) { 
			mallGoodCommentListOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
		}
		 catch (Exception e) { 
			 mallGoodCommentListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
		}
		return mallGoodCommentListOutDTO;
	}		


	/**
	 *编辑时  根据 id查询评价管理信息
	 * 
	 */
	@RequestMapping(value="/goEditMallGoodComment")
	@ResponseBody
	public MallGoodCommentOutDTO  findMallGoodCommentById(MallGoodCommentInDTO mallGoodCommentInDTO){
		MallGoodCommentOutDTO mallGoodCommentOuts = new MallGoodCommentOutDTO();
		MallGoodComment mallGoodComment=new MallGoodComment();
		try {   
			mallGoodComment.setId(mallGoodCommentInDTO.getId());
			MallGoodComment mallGoodComments=mallGoodCommentService.findMallGoodCommentById(mallGoodComment);

			if(mallGoodComments.getContent()!=null){
				if(mallGoodComments.getCommentType()==1){
					mallGoodComments.setContent(mallGoodComments.getContent());
				}else{
					String decode = Base64Utils.getFromBase64(mallGoodComments.getContent());
					mallGoodComments.setContent(decode);
				}
			}
			if(mallGoodComments!=null){
				PropertyCopyUtil.copyPropertiesExclude(mallGoodComments,mallGoodCommentOuts);
				mallGoodCommentOuts.setErrorCode(Rescode.SUCCESS, "查询成功");
				return mallGoodCommentOuts;
			}else{
				mallGoodCommentOuts.setErrorCode("-1", "未查询到记录");
			}
		} catch (Exception e) { 
			mallGoodCommentOuts.setErrorCode("-1", "处理出现异常");
			e.printStackTrace();
		}
		mallGoodCommentOuts.setErrorCode("-2", "处理失败");
		return mallGoodCommentOuts;
	}
	
	
	/**
	 * 修改评价管理评价
	 */
	@RequestMapping(value="/updateMallGoodComment")
	@ResponseBody
	public MallGoodCommentOutDTO  updateMallGoodComment(MallGoodCommentInDTO mallGoodCommentInDTO){
		MallGoodCommentOutDTO mallGoodCommentOutDTO=new MallGoodCommentOutDTO();
		MallGoodComment mallGoodComments=new MallGoodComment();
		try {   
			mallGoodComments.setId(mallGoodCommentInDTO.getId());
//
//			MallGoodComment result =mallGoodCommentService.findMallGoodCommentById(mallGoodComments);
//			PropertyCopyUtil.copyPropertiesExclude(result, mallGoodComments);

//			mallGoodComments.setId(mallGoodCommentInDTO.getId());
//			String contents = Base64Utils.getBase64(mallGoodCommentInDTO.getContent());
//			mallGoodComments.setContent(contents);
			if(mallGoodCommentInDTO.getPicUrls() !=null){
				mallGoodComments.setPicUrls(mallGoodCommentInDTO.getPicUrls());
			}else{
				mallGoodComments.setPicUrls(null);
			}
			mallGoodComments.setContent(mallGoodCommentInDTO.getContent());
			mallGoodComments.setGmtModified(new Date());
			mallGoodCommentService.updateMallGoodComment(mallGoodComments);
			mallGoodCommentOutDTO.setErrorCode(Rescode.SUCCESS, "修改成功");
			
			String content ="修改评价信息";
			sysLogService.addSysLog(mallGoodCommentInDTO.getUserId(),mallGoodCommentInDTO.getUserName(),mallGoodCommentInDTO.getIp(),content,mallGoodCommentInDTO.getGroupId());
			return mallGoodCommentOutDTO;
		} catch (Exception e) { 
			mallGoodCommentOutDTO.setErrorCode("-1", "处理出现异常");
			e.printStackTrace();
		}
		mallGoodCommentOutDTO.setErrorCode("-2", "处理失败");
		return null;
	}
	
	/**
	 * 修改状态
	 * @param mallGoodCommentInDTO
	 * @return
	 */
	@RequestMapping(value="/updateStatus")
	@ResponseBody
	public MallGoodCommentOutDTO  updateStatus(MallGoodCommentInDTO mallGoodCommentInDTO){
		MallGoodCommentOutDTO mallGoodCommentOutDTO=new MallGoodCommentOutDTO();
		try {  
			MallGoodComment mallGoodComments=mallGoodCommentService.findMallGoodComment(mallGoodCommentInDTO.getId());
			mallGoodCommentService.updateStatus(mallGoodComments);
			
			mallGoodCommentOutDTO.setErrorCode(Rescode.SUCCESS, "修改成功");
			
			String content ="修改评价信息状态";
			sysLogService.addSysLog(mallGoodCommentInDTO.getUserId(),mallGoodCommentInDTO.getUserName(),mallGoodCommentInDTO.getIp(),content,mallGoodCommentInDTO.getGroupId());
			
			return mallGoodCommentOutDTO;
		} catch (Exception e) { 
			mallGoodCommentOutDTO.setErrorCode("-1", "处理出现异常");
			e.printStackTrace();
		}
		mallGoodCommentOutDTO.setErrorCode("-2", "处理失败");
		return null;
	}
	
	//批量更新
	  @RequestMapping(value = "/updateAuditStatusList")
	    @ResponseBody
	    public List<Integer> updateAuditStatusList(@RequestBody List<MallGoodCommentInDTO> mallGoodCommentInDTOList) {
	        return mallGoodCommentService.updateAuditStatus(mallGoodCommentInDTOList);

	    }
	  

	
		/**
		 * 修改评价设置
		 * @param 
		 * @return
		 */						
		@RequestMapping(value="/updateSheZhi")
		@ResponseBody
		public MallGoodCommentOutDTO  updateMemberLoginPassword(MallGoodCommentInDTO mallGoodCommentInDTO){
			
			MallGoodCommentOutDTO mallGoodCommentOuts=new MallGoodCommentOutDTO();
//			MallGoodComment mallGoodComment = new MallGoodComment();
			GroupConfig groupConfig= new GroupConfig();
			
			try {   
				groupConfig.setGroupId(mallGoodCommentInDTO.getGroupId());
				groupConfig.setCheckStatus(mallGoodCommentInDTO.getCheckStatus());
				groupConfig.setBatchDate(new Date());
				groupConfigService.updateStatus(groupConfig);
				mallGoodCommentOuts.setErrorCode(Rescode.SUCCESS, "修改成功");
				return mallGoodCommentOuts;
			} catch (Exception e) { 
				mallGoodCommentOuts.setErrorCode("-1", "处理出现异常");
				e.printStackTrace();
			}
			mallGoodCommentOuts.setErrorCode("-2", "处理失败");
			return null;
		}
	
		
	
}
